<!DOCTYPE html>
<html lang='en'>

<head>
    <meta charset='UTF-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>不可变值</title>
    <script src='../node_modules/react/umd/react.development.js'></script>
    <script src='../node_modules/react-dom/umd/react-dom.development.js'></script>
    <script src='../node_modules/babel-standalone/babel.min.js'></script>
    <script src='../node_modules/prop-types/prop-types.js'></script>
</head>

<body>
    <div id='root'></div>
    <script type='text/babel'>
        class MyCom extends React.Component {
            constructor(props) {
                super(props)

                this.state = {
                    obj: {
                        name: "晕地理",
                        age: 39
                    }
                }
            }
            
            render() {
                return (
                    <div>
                        <div>{JSON.stringify(this.state.obj)}</div>
                        <button onClick={() => {
                            // 错误示范如下！！！！！
                            // this.state.obj.name = "钢琴小王子"

                            // this.state.obj.name = "钢琴小王子"
                            // this.setState({
                            //     obj: { ...this.state.obj }
                            // })

                            // this.setState({
                            //     obj: Object.assign(
                            //         {},
                            //         { name: this.state.obj.name },
                            //         { age: 93 },
                            //         { name: "钢琴小王子" },
                            //     )
                            // })

                            this.setState({
                                obj: Object.assign(
                                    {},
                                    { ...this.state.obj },
                                    { name: "钢琴小王子" },
                                )
                            })

                        }}>修改state</button>
                    </div>
                )
            }
        }

        ReactDOM.render(<MyCom />, document.getElementById('root'))
    </script>
</body>

</html>